#include "lcd_init.h"
// #include "delay.h"
// #include "spi.h"

#if 0


#define  LCDP_RAM_BUFF_SIZE     (LCDP_W*2)
__attribute__((aligned(4))) UINT8 LCDP_CFG_Buff[128]={0};
__attribute__((aligned(4))) UINT8 LCDP_RAM_Buff1[LCDP_RAM_BUFF_SIZE]={0};
__attribute__((aligned(4))) UINT8 LCDP_RAM_Buff2[LCDP_RAM_BUFF_SIZE]={0};

volatile uint8_t g_SpiDmaTransFlag = 0;//1：正在传输；0：结束传输；


void LCDP_GPIO_Init(void)
{
    GPIOB_SetBits(LCDP_DC_Pin |LCDP_CS_Pin |LCDP_BLK_Pin);
    GPIOB_ModeCfg(LCDP_DC_Pin |LCDP_CS_Pin |LCDP_BLK_Pin , GPIO_ModeOut_PP_5mA);
}

void MYSPI_MasterDMAITCfg(void)
{
    R8_SPI0_CTRL_MOD &= ~RB_SPI_FIFO_DIR;
    R32_SPI0_DMA_BEG = (uint32_t)1;
    R32_SPI0_DMA_END = (uint32_t)0;
    R16_SPI0_TOTAL_CNT = 0;
    R8_SPI0_INT_FLAG =0xff;
    R8_SPI0_INTER_EN |= RB_SPI_IE_DMA_END;//|RB_SPI_IE_CNT_END|RB_SPI_IE_BYTE_END|RB_SPI_IE_FST_BYTE;
    PFIC_EnableIRQ(SPI0_IRQn);
}

void MYSPI_MasterDMATrans(uint8_t *pbuf, uint16_t len)
{
    // R8_SPI0_CTRL_MOD &= ~RB_SPI_FIFO_DIR;
    R32_SPI0_DMA_BEG = (uint32_t)pbuf;
    R32_SPI0_DMA_END = (uint32_t)(pbuf + len);
    R16_SPI0_TOTAL_CNT = len;
    R8_SPI0_INT_FLAG = RB_SPI_IF_CNT_END | RB_SPI_IF_DMA_END;
    R8_SPI0_CTRL_CFG |= RB_SPI_DMA_ENABLE;
}

void LCDP_Writ_Bus_DMA(uint8_t *pbuf, uint16_t len)
{
    g_SpiDmaTransFlag = 1;
    LCDP_CS_Clr();
    // R8_SPI0_CTRL_MOD &= ~RB_SPI_FIFO_DIR;
    R32_SPI0_DMA_BEG = (uint32_t)pbuf;
    R32_SPI0_DMA_END = (uint32_t)(pbuf + len);
    R16_SPI0_TOTAL_CNT = len;
    R8_SPI0_INT_FLAG = RB_SPI_IF_CNT_END | RB_SPI_IF_DMA_END;
    R8_SPI0_CTRL_CFG |= RB_SPI_DMA_ENABLE;
    while (g_SpiDmaTransFlag ||(!(R8_SPI0_INT_FLAG & RB_SPI_FREE)) )
        ;
    LCDP_CS_Set();
}

void delay(int t)
{
    while(t--);
}
/******************************************************************************
      函数说明：LCD串行数据写入函数
      入口数据：dat  要写入的串行数据
      返回值：  无
******************************************************************************/
void LCDP_Writ_Bus (uint8_t dat) 
{
    LCDP_CS_Clr();
    R8_SPI0_CTRL_MOD &= ~RB_SPI_FIFO_DIR;
    R8_SPI0_BUFFER = dat;
    while (!(R8_SPI0_INT_FLAG & RB_SPI_FREE))
        ;
    LCDP_CS_Set();
}

/******************************************************************************
      函数说明：LCD写入数据
      入口数据：dat 写入的数据
      返回值：  无
******************************************************************************/
void LCDP_WR_DATA8(uint8_t dat)
{
    LCDP_Writ_Bus(dat);
}


/******************************************************************************
      函数说明：LCD写入数据
      入口数据：dat 写入的数据
      返回值：  无
******************************************************************************/
void LCDP_WR_DATA(uint16_t dat)
{
    // LCDP_Writ_Bus(dat>>8);
    // LCDP_Writ_Bus(dat);

    LCDP_CS_Clr();
    R8_SPI0_CTRL_MOD &= ~RB_SPI_FIFO_DIR;
    R8_SPI0_BUFFER = dat>>8;
    while (!(R8_SPI0_INT_FLAG & RB_SPI_FREE))
        ;
    R8_SPI0_BUFFER = dat;
    while (!(R8_SPI0_INT_FLAG & RB_SPI_FREE))
        ;
    LCDP_CS_Set();
}

/******************************************************************************
      函数说明：LCD写入数据
      入口数据：dat 写入的数据
      返回值：  无
******************************************************************************/
void LCDP_WR_Bytes (uint8_t *data, uint16_t datalen) {
    LCDP_CS_Clr();
    R8_SPI0_CTRL_MOD &= ~RB_SPI_FIFO_DIR;

    for (uint16_t i = 0; i < datalen; i++) {
        R8_SPI0_BUFFER = data[i];
        while (!(R8_SPI0_INT_FLAG & RB_SPI_FREE))
            ;
    }
    LCDP_CS_Set();
}

/******************************************************************************
      函数说明：LCD写入命令
      入口数据：dat 写入的命令
      返回值：  无
******************************************************************************/
void LCDP_WR_REG(uint8_t dat)
{
    LCDP_DC_Clr();//写命令
    LCDP_Writ_Bus(dat);
    LCDP_DC_Set();//写数据
}
/******************************************************************************
      函数说明：设置起始和结束地址
      入口数据：x1,x2 设置列的起始和结束地址
                y1,y2 设置行的起始和结束地址
      返回值：  无
******************************************************************************/
void LCDP_Address_Set (uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) {
#if (USE_HORIZONTAL == 0)
        {
        LCDP_WR_REG (0x2a);  // 列地址设置
        LCDP_WR_DATA (x1 + 2);
        LCDP_WR_DATA (x2 + 2);
        LCDP_WR_REG (0x2b);  // 行地址设置
        LCDP_WR_DATA (y1 + 1);
        LCDP_WR_DATA (y2 + 1);
        LCDP_WR_REG (0x2c);  // 储存器写
        }
#elif (USE_HORIZONTAL == 1)
        {
        LCDP_WR_REG (0x2a);  // 列地址设置
        LCDP_WR_DATA (x1 + 2);
        LCDP_WR_DATA (x2 + 2);
        LCDP_WR_REG (0x2b);  // 行地址设置
        LCDP_WR_DATA (y1 + 1);
        LCDP_WR_DATA (y2 + 1);
        LCDP_WR_REG (0x2c);  // 储存器写
        }
#elif (USE_HORIZONTAL == 2)
        {
        LCDP_WR_REG (0x2a);  // 列地址设置
        LCDP_WR_DATA (x1 + 1);
        LCDP_WR_DATA (x2 + 1);
        LCDP_WR_REG (0x2b);  // 行地址设置
        LCDP_WR_DATA (y1 + 2);
        LCDP_WR_DATA (y2 + 2);
        LCDP_WR_REG (0x2c);  // 储存器写
        }
#elif (USE_HORIZONTAL == 3)
        {
        LCDP_WR_REG (0x2a);  // 列地址设置
        LCDP_WR_DATA (x1 + 1);
        LCDP_WR_DATA (x2 + 1);
        LCDP_WR_REG (0x2b);  // 行地址设置
        LCDP_WR_DATA (y1 + 2);
        LCDP_WR_DATA (y2 + 2);
        LCDP_WR_REG (0x2c);  // 储存器写
        }
#endif
}
#define LCD_CS_Clr()    LCDP_CS_Clr()
#define LCD_CS_Set()    LCDP_CS_Set()
#define LCD_DC_Clr()    LCDP_DC_Clr()
#define LCD_DC_Set()    LCDP_DC_Set()

#define LCD_RES_Clr()  LCDP_RES_Clr()
#define LCD_RES_Set()   LCDP_RES_Set()

#define LCD_BLK_Clr()   LCDP_BLK_Clr()
#define LCD_BLK_Set()   LCDP_BLK_Set()


#define LCD_SCLK_Clr()  GPIOB_ResetBits(LCDP_SCL_Pin)
#define LCD_SCLK_Set()  GPIOB_SetBits(LCDP_SCL_Pin)
#define LCD_MOSI_Clr()  GPIOB_ResetBits(LCDP_SDA_Pin)
#define LCD_MOSI_Set()  GPIOB_SetBits(LCDP_SDA_Pin)

void LCDP_Init(void)
{
    uint8_t lcdcfgdata[3] = {0x07,0x30,0x30};
    LCDP_GPIO_Init();//初始化GPIO
    LCDP_BLK_Set();//打开背光
       mDelaymS(10);
       LCD_RES_Clr();//复位
       DelayMs(100);
       LCD_RES_Set();
       DelayMs(100);

       LCD_BLK_Set();//打开背光
    /* SPI 0 */
    GPIOPinRemap(ENABLE, RB_PIN_SPI0);
    GPIOB_ModeCfg(LCDP_SCL_Pin | LCDP_SDA_Pin, GPIO_ModeOut_PP_5mA);

    SPI0_MasterDefInit();
    SPI0_CLKCfg(15);
    MYSPI_MasterDMAITCfg();
    LCDP_BLK_Set();//打开背光
    DelayMs(100);
//************* Start Initial Sequence **********//
     LCDP_WR_REG(0x11); //Sleep out
     DelayMs(120);              //Delay 120ms
    //------------------------------------ST7735S Frame Rate-----------------------------------------//
     LCDP_WR_REG(0xB1); //Frame Rate Control (In normal mode/ Full colors)
         // LCDP_WR_DATA8(0x05);
         // LCDP_WR_DATA8(0x3C);
         // LCDP_WR_DATA8(0x3C);
         LCDP_WR_Bytes(lcdcfgdata, 3);
         LCDP_WR_REG(0xB2); //Frame Rate Control (In Idle mode/ 8-colors)
         // LCDP_WR_DATA8(0x05);
         // LCDP_WR_DATA8(0x3C);
         // LCDP_WR_DATA8(0x3C);
         LCDP_WR_Bytes(lcdcfgdata, 3);
         LCDP_WR_REG(0xB3); //Frame Rate Control (In Partial mode/ full colors)
         // LCDP_WR_DATA8(0x05);
         // LCDP_WR_DATA8(0x3C);
         // LCDP_WR_DATA8(0x3C);
         // LCDP_WR_DATA8(0x05);
         // LCDP_WR_DATA8(0x3C);
         // LCDP_WR_DATA8(0x3C);
         LCDP_WR_Bytes(lcdcfgdata, 3);
         LCDP_WR_Bytes(lcdcfgdata, 3);
         //------------------------------------End ST7735S Frame Rate---------------------------------//
         LCDP_WR_REG(0xB4); //Dot inversion
         LCDP_WR_DATA8(0x03);
         //------------------------------------ST7735S Power Sequence---------------------------------//
         LCDP_WR_REG(0xC0);
         // LCDP_WR_DATA8(0x28);
         // LCDP_WR_DATA8(0x08);
         // LCDP_WR_DATA8(0x04);
         uint8_t temp[3] = {0x28,0x08,0x04};
         LCDP_WR_Bytes(temp,3);
         LCDP_WR_REG(0xC1);
         LCDP_WR_DATA8(0XC0);
         LCDP_WR_REG(0xC2);
         // LCDP_WR_DATA8(0x0D);
         // LCDP_WR_DATA8(0x00);
         LCDP_WR_DATA(0x0D00);
         LCDP_WR_REG(0xC3);
         // LCDP_WR_DATA8(0x8D);
         // LCDP_WR_DATA8(0x2A);
         LCDP_WR_DATA(0x8D2A);
         LCDP_WR_REG(0xC4);
         // LCDP_WR_DATA8(0x8D);
         // LCDP_WR_DATA8(0xEE);
         LCDP_WR_DATA(0x8DEE);
         //---------------------------------End ST7735S Power Sequence-------------------------------------//
         LCDP_WR_REG(0xC5); //VCOM
         LCDP_WR_DATA8(0x1A);
         LCDP_WR_REG(0x36); //MX, MY, RGB mode

     #if (USE_HORIZONTAL == 0)
             LCDP_WR_DATA8 (0x00);
     #elif (USE_HORIZONTAL == 1)
             LCDP_WR_DATA8 (0xC0);
     #elif (USE_HORIZONTAL == 2)
             LCDP_WR_DATA8 (0x70);
     #elif (USE_HORIZONTAL == 3)
             LCDP_WR_DATA8 (0xA0);
     #endif
             //------------------------------------ST7735S Gamma Sequence---------------------------------//
             {
             LCDP_WR_REG (0xE0);
             #if 1
             uint8_t LCDP_CFG_Buff1[16] = {0x04, 0x22, 0x07, 0x0A, 0x2E, 0x30, 0x25, 0x2A, 0x28, 0x26, 0x2E, 0x3A, 0x00, 0x01, 0x03, 0x13};
             LCDP_Writ_Bus_DMA(LCDP_CFG_Buff1,16);
             #else
             LCDP_WR_DATA8(0x04);
             LCDP_WR_DATA8(0x22);
             LCDP_WR_DATA8(0x07);
             LCDP_WR_DATA8(0x0A);
             LCDP_WR_DATA8(0x2E);
             LCDP_WR_DATA8(0x30);
             LCDP_WR_DATA8(0x25);
             LCDP_WR_DATA8(0x2A);
             LCDP_WR_DATA8(0x28);
             LCDP_WR_DATA8(0x26);
             LCDP_WR_DATA8(0x2E);
             LCDP_WR_DATA8(0x3A);
             LCDP_WR_DATA8(0x00);
             LCDP_WR_DATA8(0x01);
             LCDP_WR_DATA8(0x03);
             LCDP_WR_DATA8(0x13);
             #endif
             }
             {
             LCDP_WR_REG (0xE1);
             #if 1
             uint8_t LCDP_CFG_Buff1[16] = {0x04, 0x16, 0x06, 0x0D, 0x2D, 0x26, 0x23, 0x27, 0x27, 0x25, 0x2D, 0x3B, 0x00, 0x01, 0x04, 0x13};
             LCDP_Writ_Bus_DMA(LCDP_CFG_Buff1,16);
             #else
             LCDP_WR_DATA8 (0x04);
             LCDP_WR_DATA8 (0x16);
             LCDP_WR_DATA8 (0x06);
             LCDP_WR_DATA8 (0x0D);
             LCDP_WR_DATA8 (0x2D);
             LCDP_WR_DATA8 (0x26);
             LCDP_WR_DATA8 (0x23);
             LCDP_WR_DATA8 (0x27);
             LCDP_WR_DATA8 (0x27);
             LCDP_WR_DATA8 (0x25);
             LCDP_WR_DATA8 (0x2D);
             LCDP_WR_DATA8 (0x3B);
             LCDP_WR_DATA8 (0x00);
             LCDP_WR_DATA8 (0x01);
             LCDP_WR_DATA8 (0x04);
             LCDP_WR_DATA8 (0x13);
             #endif
             }

             //------------------------------------End ST7735S Gamma Sequence-----------------------------//
         LCDP_WR_REG(0x3A); //65k mode
         LCDP_WR_DATA8(0x05);
         LCDP_WR_REG(0x29); //Display on
}

__INTERRUPT
__HIGH_CODE
void SPI0_IRQHandler (void) {

    if (SPI0_GetITFlag (RB_SPI_IF_DMA_END)) {//RB_SPI_IF_CNT_END|
        R8_SPI0_CTRL_CFG &= ~RB_SPI_DMA_ENABLE;   
        g_SpiDmaTransFlag = 0;
        R32_SPI0_DMA_BEG = (uint32_t)1;
        R32_SPI0_DMA_END = (uint32_t)0;
        SPI0_ClearITFlag (RB_SPI_IF_DMA_END);

        // PRINT ("1: %02x\n", R8_SPI0_INT_FLAG);
    }
    
}
#else
#define LCD_CS_Clr()    LCDP_CS_Clr()
#define LCD_CS_Set()    LCDP_CS_Set()
#define LCD_DC_Clr()    LCDP_DC_Clr()
#define LCD_DC_Set()    LCDP_DC_Set()

#define LCD_RES_Clr()  LCDP_RES_Clr()
#define LCD_RES_Set()   LCDP_RES_Set()

#define LCD_BLK_Clr()   LCDP_BLK_Clr()
#define LCD_BLK_Set()   LCDP_BLK_Set()


#define LCD_SCLK_Clr()  GPIOB_ResetBits(LCDP_SCL_Pin)
#define LCD_SCLK_Set()  GPIOB_SetBits(LCDP_SCL_Pin)
#define LCD_MOSI_Clr()  GPIOB_ResetBits(LCDP_SDA_Pin)
#define LCD_MOSI_Set()  GPIOB_SetBits(LCDP_SDA_Pin)

#if 1

static uint8_t lcd_power_status = 0;
void LCD_GPIO_Init(void)
{
    GPIOB_SetBits(LCDP_DC_Pin |LCDP_CS_Pin |LCDP_BLK_Pin);
    GPIOB_ModeCfg(LCDP_DC_Pin |LCDP_CS_Pin |LCDP_BLK_Pin |LCDP_RES_Pin, GPIO_ModeOut_PP_20mA);
    GPIOB_ModeCfg(LCDP_SCL_Pin |LCDP_SDA_Pin , GPIO_ModeOut_PP_20mA);

    GPIOPinRemap(ENABLE, RB_PIN_SPI0);
    //SPI0_MasterDefInit();
    {
        R8_SPI0_CLOCK_DIV = 2; // 主频时钟2分频
        R8_SPI0_CTRL_MOD = RB_SPI_ALL_CLEAR;
        R8_SPI0_CTRL_MOD = RB_SPI_MOSI_OE | RB_SPI_SCK_OE;
        R8_SPI0_CTRL_CFG |= RB_SPI_AUTO_IF;     // 访问BUFFER/FIFO自动清除IF_BYTE_END标志
        R8_SPI0_CTRL_CFG &= ~RB_SPI_DMA_ENABLE; // 不启动DMA方式
    }
   // GPIOPinRemap(ENABLE, RB_PIN_SPI0);

//      GPIO_ResetBits(SPIM0_SS_PORT, SPIM0_SS_PIN);
//      GPIO_ModeCfg(SPIM0_SS_PORT, SPIM0_SS_PIN, GPIO_ModeOut_PP_5mA);
//      GPIO_ModeCfg(SPIM0_SCK_PORT, SPIM0_SCK_PIN, GPIO_ModeOut_PP_5mA);
//      GPIO_ModeCfg(SPIM0_MOSI_PORT, SPIM0_MOSI_PIN, GPIO_ModeOut_PP_5mA);
//
//      SPI0_MasterDefInit();
      //SPI0_CLKCfg(2);
//      R8_SPI0_CTRL_CFG |= RB_SPI_MST_DLY_EN;
//      SPI0_DataMode(Mode0_HighBitINFront);
}

void LCD_power_off(void)
{
   // GPIOPinRemap(DISABLE, RB_PIN_SPI0);
    R8_SPI0_CTRL_MOD &= ~RB_SPI_MOSI_OE;
    R8_SPI0_CTRL_MOD &= ~RB_SPI_SCK_OE;

    LCD_BLK_Clr();
    LCD_CS_Clr();
    LCD_DC_Clr();
    LCD_RES_Clr();

    LCD_SCLK_Clr();
    LCD_MOSI_Clr();

    lcd_power_status = 0;
}

/******************************************************************************
      函数说明：LCD串行数据写入函数
      入口数据：dat  要写入的串行数据
      返回值：  无
******************************************************************************/
__attribute__((section(".highcode")))
void LCD_Writ_Bus(uint8_t dat)
{
  LCD_DC_Set();
  LCD_CS_Clr();
  //SPI0_MasterSendByte( dat );
  R8_SPI0_CTRL_MOD &= ~RB_SPI_FIFO_DIR;
  R8_SPI0_BUFFER = dat;
  while(!(R8_SPI0_INT_FLAG & RB_SPI_FREE));
  LCD_CS_Set();
  LCD_DC_Clr();
}


/******************************************************************************
      函数说明：LCD写入数据
      入口数据：dat 写入的数据
      返回值：  无
******************************************************************************/
__attribute__((section(".highcode")))
void LCD_WR_DATA8(uint8_t dat)
{
   LCD_DC_Set();
   LCD_CS_Clr();
   //SPI0_MasterSendByte( dat );
   R8_SPI0_CTRL_MOD &= ~RB_SPI_FIFO_DIR;
   R8_SPI0_BUFFER = dat;
   while(!(R8_SPI0_INT_FLAG & RB_SPI_FREE));
   LCD_CS_Set();
   LCD_DC_Clr();
}

uint8_t TxData[2] = {0};
/******************************************************************************
      函数说明：LCD写入数据
      入口数据：dat 写入的数据
      返回值：  无
******************************************************************************/
__attribute__((section(".highcode")))
void LCD_WR_DATA(uint16_t dat)
{
//    LCD_Writ_Bus((dat>>8)&0xFF);
//    LCD_Writ_Bus(dat);
    TxData[1] = dat & 0xff;                            //往缓冲区中写入数据  低位
    TxData[0] = (dat >> 8) & 0xff;                     //往缓冲区中写入数据  高
    LCD_DC_Set();
      LCD_CS_Clr();
      uint8_t len = sizeof(dat);
      {
          R8_SPI0_CTRL_MOD &= ~RB_SPI_FIFO_DIR;
          R32_SPI0_DMA_BEG = (uint32_t)(TxData);
          R32_SPI0_DMA_END = (uint32_t)(TxData+len);
          R16_SPI0_TOTAL_CNT = len;
          R8_SPI0_INT_FLAG = RB_SPI_IF_CNT_END | RB_SPI_IF_DMA_END;
          R8_SPI0_CTRL_CFG |= RB_SPI_DMA_ENABLE;
          while(!(R8_SPI0_INT_FLAG & RB_SPI_IF_CNT_END));
          R8_SPI0_CTRL_CFG &= ~RB_SPI_DMA_ENABLE;
          LCD_CS_Set();
      }
}


/******************************************************************************
      函数说明：LCD写入命令
      入口数据：dat 写入的命令
      返回值：  无
******************************************************************************/
__attribute__((section(".highcode")))
void LCD_WR_REG(uint8_t dat)
{
    LCD_CS_Clr();
    LCD_DC_Clr();//写命令
    R8_SPI0_CTRL_MOD &= ~RB_SPI_FIFO_DIR;
    R8_SPI0_BUFFER = dat;
    while(!(R8_SPI0_INT_FLAG & RB_SPI_FREE));
    LCD_DC_Set();//写数据
    LCD_CS_Set();
}


/******************************************************************************
      函数说明：设置起始和结束地址
      入口数据：x1,x2 设置列的起始和结束地址
                y1,y2 设置行的起始和结束地址
      返回值：  无
******************************************************************************/
void LCDP_Address_Set(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2)
{
#if(USE_HORIZONTAL==0)
    {
        LCD_WR_REG(0x2a);//列地址设置
        LCD_WR_DATA(x1+2);
        LCD_WR_DATA(x2+2);
        LCD_WR_REG(0x2b);//行地址设置
        LCD_WR_DATA(y1+1);
        LCD_WR_DATA(y2+1);
        LCD_WR_REG(0x2c);//储存器写
    }
#elif(USE_HORIZONTAL==1)
    {
        LCD_WR_REG(0x2a);//列地址设置
        LCD_WR_DATA(x1+2);
        LCD_WR_DATA(x2+2);
        LCD_WR_REG(0x2b);//行地址设置
        LCD_WR_DATA(y1+1);
        LCD_WR_DATA(y2+1);
        LCD_WR_REG(0x2c);//储存器写
    }
#elif(USE_HORIZONTAL==2)
    {
        LCD_WR_REG(0x2a);//列地址设置
        LCD_WR_DATA(x1);
        LCD_WR_DATA(x2);
        LCD_WR_REG(0x2b);//行地址设置
        LCD_WR_DATA(y1);
        LCD_WR_DATA(y2);
        LCD_WR_REG(0x2c);//储存器写
    }
#elif (USE_HORIZONTAL == 3)
    {
        LCD_WR_REG(0x2a);//列地址设置
        LCD_WR_DATA(x1);
        LCD_WR_DATA(x2);
        LCD_WR_REG(0x2b);//行地址设置
        LCD_WR_DATA(y1);
        LCD_WR_DATA(y2);
        LCD_WR_REG(0x2c);//储存器写
    }
#endif
}

void LCDP_Init(void)
{
    if(lcd_power_status){
        return;
    }
    LCD_GPIO_Init();//初始化GPIO
    LCD_BLK_Set();//打开背光
    mDelaymS(10);
    LCD_RES_Clr();//复位
    mDelaymS(100);
    LCD_RES_Set();
    mDelaymS(100);

    LCD_BLK_Set();//打开背光
   // mDelaymS(100);

    //************* Start Initial Sequence **********//
    LCD_WR_REG(0x11); //Sleep out
    mDelaymS(120);              //Delay 120ms
    //------------------------------------ST7735S Frame Rate-----------------------------------------//
    LCD_WR_REG(0xB1);
    LCD_WR_DATA8(0x05);
    LCD_WR_DATA8(0x3C);
    LCD_WR_DATA8(0x3C);
    LCD_WR_REG(0xB2);
    LCD_WR_DATA8(0x05);
    LCD_WR_DATA8(0x3C);
    LCD_WR_DATA8(0x3C);
    LCD_WR_REG(0xB3);
    LCD_WR_DATA8(0x05);
    LCD_WR_DATA8(0x3C);
    LCD_WR_DATA8(0x3C);
    LCD_WR_DATA8(0x05);
    LCD_WR_DATA8(0x3C);
    LCD_WR_DATA8(0x3C);
    //------------------------------------End ST7735S Frame Rate---------------------------------//
    LCD_WR_REG(0xB4); //Dot inversion
    LCD_WR_DATA8(0x03);
    //------------------------------------ST7735S Power Sequence---------------------------------//
    LCD_WR_REG(0xC0);
    LCD_WR_DATA8(0x28);
    LCD_WR_DATA8(0x08);
    LCD_WR_DATA8(0x04);
    LCD_WR_REG(0xC1);
    LCD_WR_DATA8(0XC0);
    LCD_WR_REG(0xC2);
    LCD_WR_DATA8(0x0D);
    LCD_WR_DATA8(0x00);
    LCD_WR_REG(0xC3);
    LCD_WR_DATA8(0x8D);
    LCD_WR_DATA8(0x2A);
    LCD_WR_REG(0xC4);
    LCD_WR_DATA8(0x8D);
    LCD_WR_DATA8(0xEE);
    //---------------------------------End ST7735S Power Sequence-------------------------------------//
    LCD_WR_REG(0xC5); //VCOM
    LCD_WR_DATA8(0x1A);
    LCD_WR_REG(0x36); //MX, MY, RGB mode
#if(USE_HORIZONTAL==0)
    LCD_WR_DATA8(0x00);
#elif (USE_HORIZONTAL==1)
    LCD_WR_DATA8(0xC0);
#elif (USE_HORIZONTAL==2)
    LCD_WR_DATA8(0x70);
#else
    LCD_WR_DATA8(0xA0);
#endif
    //------------------------------------ST7735S Gamma Sequence---------------------------------//
    LCD_WR_REG(0xE0);
    LCD_WR_DATA8(0x04);
    LCD_WR_DATA8(0x22);
    LCD_WR_DATA8(0x07);
    LCD_WR_DATA8(0x0A);
    LCD_WR_DATA8(0x2E);
    LCD_WR_DATA8(0x30);
    LCD_WR_DATA8(0x25);
    LCD_WR_DATA8(0x2A);
    LCD_WR_DATA8(0x28);
    LCD_WR_DATA8(0x26);
    LCD_WR_DATA8(0x2E);
    LCD_WR_DATA8(0x3A);
    LCD_WR_DATA8(0x00);
    LCD_WR_DATA8(0x01);
    LCD_WR_DATA8(0x03);
    LCD_WR_DATA8(0x13);
    LCD_WR_REG(0xE1);
    LCD_WR_DATA8(0x04);
    LCD_WR_DATA8(0x16);
    LCD_WR_DATA8(0x06);
    LCD_WR_DATA8(0x0D);
    LCD_WR_DATA8(0x2D);
    LCD_WR_DATA8(0x26);
    LCD_WR_DATA8(0x23);
    LCD_WR_DATA8(0x27);
    LCD_WR_DATA8(0x27);
    LCD_WR_DATA8(0x25);
    LCD_WR_DATA8(0x2D);
    LCD_WR_DATA8(0x3B);
    LCD_WR_DATA8(0x00);
    LCD_WR_DATA8(0x01);
    LCD_WR_DATA8(0x04);
    LCD_WR_DATA8(0x13);
    //------------------------------------End ST7735S Gamma Sequence-----------------------------//
    LCD_WR_REG(0x3A); //65k mode
    LCD_WR_DATA8(0x05);
    LCD_WR_REG(0x29); //Display on

    lcd_power_status = 1;
}

#else
/******************************************************************************
      گ˽˵ķúLCDԮѐ˽ߝдɫگ˽
      ɫࠚ˽ߝúdat  ҪдɫքԮѐ˽ߝ
      ׵ܘֵú  Ϟ
******************************************************************************/
void LCD_Writ_Bus(uint8_t dat)
{
    uint8_t i;
    LCD_CS_Clr();
    for(i=0;i<8;i++)
    {
        LCD_SCLK_Clr();
        if(dat&0x80)
        {
           LCD_MOSI_Set();
        }
        else
        {
           LCD_MOSI_Clr();
        }
        LCD_SCLK_Set();
        dat<<=1;
    }
  LCD_CS_Set();
}


/******************************************************************************
      گ˽˵ķúLCDдɫ˽ߝ
      ɫࠚ˽ߝúdat дɫք˽ߝ
      ׵ܘֵú  Ϟ
******************************************************************************/
void LCD_WR_DATA8(uint8_t dat)
{
    LCD_Writ_Bus(dat);
}


/******************************************************************************
      گ˽˵ķúLCDдɫ˽ߝ
      ɫࠚ˽ߝúdat дɫք˽ߝ
      ׵ܘֵú  Ϟ
******************************************************************************/
void LCD_WR_DATA(uint16_t dat)
{
    LCD_Writ_Bus(dat>>8);
    LCD_Writ_Bus(dat);
}


/******************************************************************************
      گ˽˵ķúLCDдɫļ®
      ɫࠚ˽ߝúdat дɫքļ®
      ׵ܘֵú  Ϟ
******************************************************************************/
void LCD_WR_REG(uint8_t dat)
{
    LCD_DC_Clr();//дļ®
    LCD_Writ_Bus(dat);
    LCD_DC_Set();//д˽ߝ
}


/******************************************************************************
      گ˽˵ķúʨ׃ǰʼۍޡ˸ַ֘
      ɫࠚ˽ߝúx1,x2 ʨ׃քǰʼۍޡ˸ַ֘
                y1,y2 ʨ׃ѐքǰʼۍޡ˸ַ֘
      ׵ܘֵú  Ϟ
******************************************************************************/
void LCD_Address_Set(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2)
{
    if(USE_HORIZONTAL==0)
    {
        LCD_WR_REG(0x2a);//ַ֘ʨ׃
        LCD_WR_DATA(x1+2);
        LCD_WR_DATA(x2+2);
        LCD_WR_REG(0x2b);//ѐַ֘ʨ׃
        LCD_WR_DATA(y1+1);
        LCD_WR_DATA(y2+1);
        LCD_WR_REG(0x2c);//ԢզǷд
    }
    else if(USE_HORIZONTAL==1)
    {
        LCD_WR_REG(0x2a);//ַ֘ʨ׃
        LCD_WR_DATA(x1+2);
        LCD_WR_DATA(x2+2);
        LCD_WR_REG(0x2b);//ѐַ֘ʨ׃
        LCD_WR_DATA(y1+1);
        LCD_WR_DATA(y2+1);
        LCD_WR_REG(0x2c);//ԢզǷд
    }
    else if(USE_HORIZONTAL==2)
    {
        LCD_WR_REG(0x2a);//ַ֘ʨ׃
        LCD_WR_DATA(x1+1);
        LCD_WR_DATA(x2+1);
        LCD_WR_REG(0x2b);//ѐַ֘ʨ׃
        LCD_WR_DATA(y1+2);
        LCD_WR_DATA(y2+2);
        LCD_WR_REG(0x2c);//ԢզǷд
    }
    else
    {
        LCD_WR_REG(0x2a);//ַ֘ʨ׃
        LCD_WR_DATA(x1+1);
        LCD_WR_DATA(x2+1);
        LCD_WR_REG(0x2b);//ѐַ֘ʨ׃
        LCD_WR_DATA(y1+2);
        LCD_WR_DATA(y2+2);
        LCD_WR_REG(0x2c);//ԢզǷд
    }
}

void LCDP_Init(void)
{
    LCDP_GPIO_Init();//ԵʼۯGPIO

    LCD_RES_Clr();//شλ
    DelayMs(100);
    LCD_RES_Set();
    DelayMs(100);

    LCD_BLK_Set();//ղߪѳڢ
  DelayMs(100);

    //************* Start Initial Sequence **********//
    LCD_WR_REG(0x11); //Sleep out
    DelayMs(120);              //Delay 120ms
    //------------------------------------ST7735S Frame Rate-----------------------------------------//
    LCD_WR_REG(0xB1);
    LCD_WR_DATA8(0x05);
    LCD_WR_DATA8(0x3C);
    LCD_WR_DATA8(0x3C);
    LCD_WR_REG(0xB2);
    LCD_WR_DATA8(0x05);
    LCD_WR_DATA8(0x3C);
    LCD_WR_DATA8(0x3C);
    LCD_WR_REG(0xB3);
    LCD_WR_DATA8(0x05);
    LCD_WR_DATA8(0x3C);
    LCD_WR_DATA8(0x3C);
    LCD_WR_DATA8(0x05);
    LCD_WR_DATA8(0x3C);
    LCD_WR_DATA8(0x3C);
    //------------------------------------End ST7735S Frame Rate---------------------------------//
    LCD_WR_REG(0xB4); //Dot inversion
    LCD_WR_DATA8(0x03);
    //------------------------------------ST7735S Power Sequence---------------------------------//
    LCD_WR_REG(0xC0);
    LCD_WR_DATA8(0x28);
    LCD_WR_DATA8(0x08);
    LCD_WR_DATA8(0x04);
    LCD_WR_REG(0xC1);
    LCD_WR_DATA8(0XC0);
    LCD_WR_REG(0xC2);
    LCD_WR_DATA8(0x0D);
    LCD_WR_DATA8(0x00);
    LCD_WR_REG(0xC3);
    LCD_WR_DATA8(0x8D);
    LCD_WR_DATA8(0x2A);
    LCD_WR_REG(0xC4);
    LCD_WR_DATA8(0x8D);
    LCD_WR_DATA8(0xEE);
    //---------------------------------End ST7735S Power Sequence-------------------------------------//
    LCD_WR_REG(0xC5); //VCOM
    LCD_WR_DATA8(0x1A);
    LCD_WR_REG(0x36); //MX, MY, RGB mode
    if(USE_HORIZONTAL==0)LCD_WR_DATA8(0x00);
    else if(USE_HORIZONTAL==1)LCD_WR_DATA8(0xC0);
    else if(USE_HORIZONTAL==2)LCD_WR_DATA8(0x70);
    else LCD_WR_DATA8(0xA0);
    //------------------------------------ST7735S Gamma Sequence---------------------------------//
    LCD_WR_REG(0xE0);
    LCD_WR_DATA8(0x04);
    LCD_WR_DATA8(0x22);
    LCD_WR_DATA8(0x07);
    LCD_WR_DATA8(0x0A);
    LCD_WR_DATA8(0x2E);
    LCD_WR_DATA8(0x30);
    LCD_WR_DATA8(0x25);
    LCD_WR_DATA8(0x2A);
    LCD_WR_DATA8(0x28);
    LCD_WR_DATA8(0x26);
    LCD_WR_DATA8(0x2E);
    LCD_WR_DATA8(0x3A);
    LCD_WR_DATA8(0x00);
    LCD_WR_DATA8(0x01);
    LCD_WR_DATA8(0x03);
    LCD_WR_DATA8(0x13);
    LCD_WR_REG(0xE1);
    LCD_WR_DATA8(0x04);
    LCD_WR_DATA8(0x16);
    LCD_WR_DATA8(0x06);
    LCD_WR_DATA8(0x0D);
    LCD_WR_DATA8(0x2D);
    LCD_WR_DATA8(0x26);
    LCD_WR_DATA8(0x23);
    LCD_WR_DATA8(0x27);
    LCD_WR_DATA8(0x27);
    LCD_WR_DATA8(0x25);
    LCD_WR_DATA8(0x2D);
    LCD_WR_DATA8(0x3B);
    LCD_WR_DATA8(0x00);
    LCD_WR_DATA8(0x01);
    LCD_WR_DATA8(0x04);
    LCD_WR_DATA8(0x13);
    //------------------------------------End ST7735S Gamma Sequence-----------------------------//
    LCD_WR_REG(0x3A); //65k mode
    LCD_WR_DATA8(0x05);
    LCD_WR_REG(0x29); //Display on
}
#endif
#endif






